package by.averianov.discounta.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.NoResultException;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.averianov.discounta.datamodel.Card;
import by.averianov.discounta.datamodel.CardType;
import by.averianov.discounta.datamodel.Shop;
import by.averianov.discounta.datamodel.UserAccount;
import by.averianov.discounta.datamodel.UserProfile;
import by.averianov.discounta.datamodel.enums.AccessGroup;
import by.averianov.discounta.services.UserService;

@Service
public class AdministrationServiceImpl extends UserServiceImpl implements UserService {

	private static final Logger LOGGER = LoggerFactory.getLogger(AdministrationServiceImpl.class);
	
	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. 
		// Can be used for any initialization process.
		LOGGER.info("Instance of AdministrationService is created. Class is: {}", getClass().getName());
	}	
	
	@Override
	public List<Shop> getAllShop() {
		return shopDao.getAll();
	}
	
	@Override
	public CardType getCardTypeById(Long id) {
		return cardTypeDao.getById(id);
	}
	
	@Override
	public Long getCardTypeCount() {
		return cardTypeDao.getCount();
	}
	
	@Override
	public void setShop(Shop shop) {
		Validate.isTrue(shop.getId() == null, "This method should be called for 'not saved yet' profile only. Use UPDATE instead");
		LOGGER.debug("Save new shop: {}", shop.getNameShop());
		shopDao.insert(shop);
	}
	
	@Override
	public UserAccount createUserAccount(String email, String password, Boolean userStatus, String activateCode) {
		UserAccount userAccount;
		try {
		//if(getAccountByEmail(email) != null) {
			userAccount= getAccountByEmail(email);
			LOGGER.debug(" Change Account " + userAccount.getUserEmail());
		//} else {
		} catch (NoResultException e) {
			userAccount = new UserAccount();
			LOGGER.debug(" Create Account " + userAccount.getUserEmail());
		}
		userAccount.setUserEmail(email);
		userAccount.setUserPassword(password);
		userAccount.setUserStatus(userStatus);
		userAccount.setActivateCode(activateCode);
		return userAccount;
	}
	
	@Override
	public UserProfile createUserProfile(Long id, String firstName, String lastName, Long dateCreated, Long phone, AccessGroup accessGroup) {
		final UserProfile userProfile = createUserProfile(id, firstName, lastName, dateCreated, phone);
		userProfile.setAccessGroup(accessGroup);
		LOGGER.debug(" Create Profile " + userProfile.getUserFirstName());
		return userProfile;
	}
	

	@Override
	public void removeUser(Long id) {
		profileDao.delete(id);
		accountDao.delete(id);
	}
	
	@Override
	public void removeCardType(Long id) {
		cardTypeDao.delete(id);
	}
	

	@Override
	public void removeAllUsers() {
		accountDao.deleteAll();
		profileDao.deleteAll();
	}
	
	@Override
	public void removeAllCards() {
		cardDao.deleteAll();
	}
	
	@Override
	public void removeAllCardTypes() {
		cardTypeDao.deleteAll();
	}
	
	@Override
	public void removeShop(Long id) {
    	shopDao.delete(id);
    }

	@Override
	public void removeCard(Long id) {
    	cardDao.delete(id);
    }
    
	@Override
	public void removeAllShops() {
		shopDao.deleteAll();
    }
	
	@Override
	public void saveOrUpdateCardType(CardType cardType) {
		if (cardType.getId() == null) {
            LOGGER.debug("Save new: {}", cardType);
            cardTypeDao.insert(cardType);
        } else {
            LOGGER.debug("Update: {}", cardType);
            cardTypeDao.update(cardType);
        }
	}
	
	@Override
	public void saveOrUpdateCard(Card card) {
		if (card.getId() == null) {
            LOGGER.debug("Save new: {}", card);
            cardDao.insert(card);
        } else {
            LOGGER.debug("Update: {}", card);
            cardDao.update(card);
        }
	}

}
